package org.PR.gui.elements;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class GriddedPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private GridBagConstraints constraints = null;
	
	private static final int C_HORZ = GridBagConstraints.HORIZONTAL;
	private static final int C_NONE = GridBagConstraints.NONE;
	private static final int C_WEST = GridBagConstraints.WEST;
	private static final int C_WIDTH = 1;
	private static final int C_HEIGHT = 1;
	
	public GriddedPanel() {
		this(new Insets(2, 2, 2, 2));
	}

	public GriddedPanel(Insets insets) {
		super(new GridBagLayout());
		constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets = insets;
	}
	
	public void addComponent(JComponent component, int row, int col)
	{
		addComponent(component, row, col, C_WIDTH, C_HEIGHT, C_WEST, C_NONE);
	}
	
	public void addComponent(JComponent component, int row, int col, int width, int height)
	{
		addComponent(component, row, col, width, height, C_WEST, C_NONE);
	}
	
	public void addAnchoredComponent(JComponent component, int row, int col, int anchor)
	{
		addComponent(component, row, col, C_WIDTH, C_HEIGHT, anchor, C_NONE);
	}
	
	public void addAnchoredComponent(JComponent component, int row, int col,
			int width, int height, int anchor)
	{
		addComponent(component, row, col, width, height, anchor, C_NONE);
	}
	
	public void addFilledComponent(JComponent component, int row, int col)
	{
		addComponent(component, row, col, C_WIDTH, C_HEIGHT, C_WEST, C_HORZ);
	}
	
	public void addFilledComponent(JComponent component, int row, int col, int fill)
	{
		addComponent(component, row, col, C_WIDTH, C_HEIGHT, C_WEST, fill);
	}
	
	public void addFilledComponent(JComponent component, int row, int col,
			int width, int height, int fill)
	{
		addComponent(component, row, col, width, height, C_WEST, fill);
	}

	public void addComponent(JComponent component, int row, int col, int width, int height, int anchor, int fill)
	{
		constraints.gridx = col;
		constraints.gridy = row;
		constraints.gridheight = height;
		constraints.gridwidth = width;
		constraints.anchor = anchor;
		
		double weightx = 0.0;
		double weighty = 0.0;
		
		if(width > 1)
		      weightx = 1.0;
		if(height > 1)
		      weighty = 1.0;
		
		switch(fill)
		{
			case GridBagConstraints.HORIZONTAL:
				constraints.weightx = weightx;
				constraints.weighty = 0.0;
				break;
			case GridBagConstraints.VERTICAL:
				constraints.weightx = 0.0;
				constraints.weighty = weighty;
				break;
			case GridBagConstraints.BOTH:
				constraints.weightx = weightx;
				constraints.weighty = weighty;
				break;
			case GridBagConstraints.NONE:
				constraints.weightx = 0.0;
				constraints.weighty = 0.0;
				break;
			default:
				break;
		}
		constraints.fill = fill;
		add(component, constraints);
	}
}
